<?php

/**
 * @author Watanabex
 * @copyright 2012
 * masmonitos_albums DAO
 */
require('to/Masmonitos_Album.php');
include_once 'masmonitos_imagenesDAO.php';

class masmonitos_AlbumsDAO{
	const SEL_ALBUM = "SELECT * FROM masmonitos_albums WHERE status = 'A' ORDER BY fechaPublicacion DESC, id DESC LIMIT 1;";
	const SEL_TOPTEN = "SELECT * FROM masmonitos_albums WHERE status = 'A' ORDER BY fechaPublicacion DESC, id DESC LIMIT 10;";
	
	public static function getUltimo(){
		$sSql = self::SEL_ALBUM;
		
		$DB = DBFunctions::getInstance();
		$arrNotas = $DB->execSelect($sSql, array());
		
		return self::populate($arrNotas);
	}
	
	public static function getTop10(){
		$sSql = self::SEL_TOPTEN;
		
		$DB = DBFunctions::getInstance();
		$arrNotas = $DB->execSelect($sSql, array());
		
		return self::populate($arrNotas);
	}	
	
	/*This function get's the result array and builds a masmonitos_albums array object
	 * @param array Masmonitos_Album result
	 * @returns array with Masmonitos_Album objects */
	private static function populate($arrMasmonitosAlbum){
		
		$objMasmonitosAlbums = array();
		
		foreach($arrMasmonitosAlbum as $A){
			$objMasmonitosAlbum = new Masmonitos_Album();
			$objMasmonitosAlbum->setID($A["id"]);
			$objMasmonitosAlbum->setName($A["name"]);
			$objMasmonitosAlbum->setFechaPublicacion($A["fechaPublicacion"]);
			$objMasmonitosAlbum->setStatus($A["status"]);
			
			$objMasmonitosImagenes = masmonitos_imagenesDAO::getByAlbum($A["id"]);
			
			$objMasmonitosAlbum->setFotos($objMasmonitosImagenes);
			
			array_push($objMasmonitosAlbums, $objMasmonitosAlbum);
		}
		return $objMasmonitosAlbums;
	}
}